package cn.qingfeng.a002_chat.server;

import cn.qingfeng.a002_chat.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

//@Slf4j
public class ChatServer {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            new ServerBootstrap()
                    .channel(NioServerSocketChannel.class)
                    .group(boss, worker)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(new StringEncoder());
                            sc.pipeline().addLast(new StringDecoder());
                            sc.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));

                            sc.pipeline().addLast(new LoginHandler());
                            sc.pipeline().addLast(new SendHandler());
                            sc.pipeline().addLast(new GsendHandler());
                            sc.pipeline().addLast(new GcreateHandler());
                            sc.pipeline().addLast(new GmembersHandler());
                            sc.pipeline().addLast(new GjoinHandler());
                            sc.pipeline().addLast(new GquitHandler());
                            sc.pipeline().addLast(new QuitHandler());
                        }
                    })
                    .bind(8080)
                    .sync()
                    .channel()
                    .closeFuture()
                    .sync();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}
